package com.baizhi.lhr.service;

import com.baizhi.lhr.dao.AdminDao;
import com.baizhi.lhr.entity.Admin;
import com.baizhi.lhr.util.ImageCodeUtil;
import com.baizhi.lhr.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void register(Admin admin) {
        String password = admin.getPassword();
        //调工具类 获取随机盐
        String salt = Md5Utils.getSalt(6);
        //对原始密码 加随机盐
        String saltPassword = salt +password +salt;
        //调用工具类 对加随机盐后的密码 进行MD5加密
        String md5Code = Md5Utils.getMd5Code(saltPassword);

        //对传递过来的管理员数据 进行数据加工
        admin.setPassword(md5Code);
        admin.setSalt(salt);

        adminDao.insert(admin);
    }
    //生成验证码
    @Override
    public Map<String,String> getImageCodes() throws IOException {
        Map<String,String> map = new HashMap<>();
        //调用工具类 获取随机验证码字符
        String code = ImageCodeUtil.getSecurityCode();
        //调用工具类 根据随机码 生成图片base64形式
        String imgBase64 = ImageCodeUtil.careateImgBase64(code);
        //把imgBase64图片信息存入map
        map.put("imgCode",imgBase64);

        //获取UUID
        String uuid = UUID.randomUUID().toString();
        //把uuid存入map
        map.put("token",uuid);

        //往redis中存验证码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("CODE_"+uuid,code,1, TimeUnit.MINUTES);
        return  map;
    }
    //登录检查
    @Override
    public Map<String,String> login(@RequestBody Admin admin, String token){
        Map<String,String> map = new HashMap<>();
        //从redis服务器上获取验证码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object obj = valueOperations.get("CODE_" + token);

        //检查验证码
        if(obj==null){//验证码过期
            map.put("message","验证码过期");
            map.put("state","error");
            return map;
        }
        //将redis中验证码装换为String
        String serverCode = obj.toString();
        if(!admin.getEnCode().equals(serverCode)){
            map.put("message","验证码错误");
            map.put("state","error");
            return map;
        }
        //验证码正确 检查账号密码
        Admin user = adminDao.selectOneByName(admin.getUsername());
        if(user==null){
            map.put("message","该用户不存在");
            map.put("state","error");
            return map;
        }else {//账号存在
            if(user.getStatus()=="0"){
               map.put("message","用户已被冻结");
               map.put("state","error");
               return map;
            }
            //获取输入的密码
            String password = admin.getPassword();
            //加DB相同的盐
            String saltPwd = user.getSalt() + password + user.getSalt();
            //调用工具类 对加盐的密码 进行二次加密
            String md5Code = Md5Utils.getMd5Code(saltPwd);
            //对传过来管理员数据进行加工
            admin.setPassword(md5Code);
            if(md5Code.equals((user.getPassword()))){
                map.put("state","success");
                map.put("message",token);
                valueOperations.set("TOKEN_"+token,user);
            }else {

            }
        }
        return map;
    }
    //获取登录用户信息
    @Override
    public Admin getUserInfo(String token) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        return (Admin)valueOperations.get("TOKEN_"+token);
    }
    //退出
    @Override
    public void logout(String token) {
        //从redis中删除 登录
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.delete("TOKEN_"+token);
    }
}
